package com.wending.fragment;

/**
 * Created by 张英杰 on 2017/1/23.
 */

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wending.R;
import com.wending.view.ProgressWebView;

/**
 * Created by 张英杰 on 2017/1/23.
 */

public class RegisterFragment extends Fragment {

    private ProgressWebView mWebView;
    public boolean dispose = false;//是否处理 回退事件
    private View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_register, container, false);
        init(view);
        return view;
    }


    private void init(View view) {
        mWebView = (ProgressWebView) view.findViewById(R.id.wv);
        String weburl = getArguments().getString("data");
        Log.i("webFragment===webUrl ", weburl);
        WebSettings settings = mWebView.getSettings();
//        设置网页布局类型：
//        *  1、LayoutAlgorithm.NARROW_COLUMNS ： 适应内容大小
//        *  2、LayoutAlgorithm.SINGLE_COLUMN: 适应屏幕，内容将自动缩放  有些网页混乱！！！飞起
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);//设置缓存
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);

        //WebView加载web资源
        mWebView.loadUrl(weburl);
//                //加载html
//               webView.loadData(weburl,"text/html","utf-8");//在有些手机上会出现乱码，将方法变成
//                webView.loadDataWithBaseURL(null,weburl,"text/html","utf-8",null);//就可以解决这个
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.d("title===", "" + title);
            }
        });
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    Log.i("web返回键===", "KEYCODE_BACK==" + (keyCode == KeyEvent.KEYCODE_BACK)
                            + "      webView.canGoBack()==" + mWebView.canGoBack());

                    if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {  //表示按返回键
                        mWebView.goBack();   //后退
                        dispose = true;
                        return true;    //已处理
                    }
                    dispose = false;
                    return false;
                }
                dispose = false;
                return false;
            }
        });
    }
}

